/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seari.bean.DataTypeConfig;
import com.seari.bean.MaterialCaregory;
import com.seari.bean.MaterialCaregoryAttr;
import com.seari.bean.Product;
import com.seari.pojo.DataGridBean;
import com.seari.pojo.MessageBean;
import com.seari.service.DataTypeConfigService;
import com.seari.service.MaterialCaregoryAttrService;
import com.seari.service.MaterialCaregoryService;
import com.seari.service.ProductService;
import com.seari.utils.StringHelper;

/**
 * 设备分类
 * @author whl
 */
@Controller
@RequestMapping("materialCaregory")
public class MaterialCaregoryController
{
    @Autowired
    private MaterialCaregoryService materialCaregoryService;

    @Autowired
    private MaterialCaregoryAttrService materialCaregoryAttrService;

    @Autowired
    private DataTypeConfigService dataTypeConfigService;

    @Autowired
    private ProductService productService;


    @RequestMapping("getFMcByFMcId")
    @ResponseBody
    public Map<String,Object> getFMcByFMcId(String fMcId){
        Map<String,Object> message = new HashMap<>();
        try{
            MaterialCaregory materialCaregoryp = new MaterialCaregory();
            materialCaregoryp.setfMcId(fMcId);
            MaterialCaregory materialCaregory = materialCaregoryService.getFMcByFMcId(materialCaregoryp);
            message.put("code",200);
            message.put("data",materialCaregory);
        }catch (Exception e){
            message.put("code",-1);
            message.put("message","请求异常!"+e.getMessage());
        }
        return message;
    }

    @RequestMapping("/getFMcTree")
    @ResponseBody
    public DataGridBean<MaterialCaregory> getFMcTree(HttpServletRequest req)
    {
        DataGridBean<MaterialCaregory> grid = new DataGridBean<MaterialCaregory>();
        String fStatus = req.getParameter("fStatus");
        String fParent = req.getParameter("fParent");
        MaterialCaregory mc = new MaterialCaregory();
        if(StringUtils.isNotBlank(fStatus))
        {
            mc.setfStatus(fStatus);
        }
        if(StringUtils.isNotBlank(fParent))
        {
            mc.setfParent(fParent);
        }

        List<MaterialCaregory> materialCaregorys = materialCaregoryService.getFMcTree(mc);
        grid.setRows(materialCaregorys);

        Map<String,Object> status = new HashMap();
        Map<String,List<MaterialCaregory>> basicData = new HashMap();
        basicData.put("basicDataList", materialCaregorys);
        status.put("code", 200);
        status.put("message", "操作成功！");
        grid.setStatus(status);
        grid.setBasicData(basicData);

        return  grid;
    }

    @RequestMapping("getMcAttrList")
    @ResponseBody
    public DataGridBean<MaterialCaregoryAttr> getMcAttrList(HttpServletRequest req)
    {
        DataGridBean<MaterialCaregoryAttr> grid = new DataGridBean<MaterialCaregoryAttr>();
        String fMcId = req.getParameter("fMcId");
        String currPage = req.getParameter("page");
        String pageSize = req.getParameter("limit");

        MaterialCaregoryAttr mcAttr = new MaterialCaregoryAttr();
        if(StringUtils.isNotBlank(fMcId))
        {
            mcAttr.setfMcId(fMcId);
        }

        if(StringUtils.isNotBlank(currPage) && StringUtils.isNotBlank(pageSize))
        {
            mcAttr.setStart((Integer.parseInt(currPage) - 1) * Integer.parseInt(pageSize));
            mcAttr.setLimit(Integer.parseInt(pageSize));
        }
        List<MaterialCaregoryAttr> mcAttrList = materialCaregoryAttrService.getFMcAttrByParams(mcAttr);
        grid.setRows(mcAttrList);
        grid.setTotal(materialCaregoryAttrService.getMcAttrListCount(mcAttr));
        return  grid;
    }

    @RequestMapping("/getDataTypeList")
    @ResponseBody
    public DataGridBean<DataTypeConfig> getDataTypeConfig(HttpServletRequest req)
    {
        DataGridBean<DataTypeConfig> grid = new DataGridBean<DataTypeConfig>();
        String fType = req.getParameter("fType");
        DataTypeConfig dt = new DataTypeConfig();
        if(StringUtils.isNotBlank(fType))
        {
            dt.setfType(fType);
        }

        List<DataTypeConfig> dataTypeConfigList = dataTypeConfigService.getDataTypeByParams(dt);
        grid.setRows(dataTypeConfigList);
        return  grid;
    }

    @RequestMapping("saveMc")
    @ResponseBody
    public MessageBean saveMc(HttpServletRequest req,MaterialCaregory materialCaregory)
    {
        MessageBean mb = new MessageBean();
        String fMcShowName = materialCaregory.getfMcShowName();
        String fMcName = materialCaregory.getfMcName();
        String fParent = materialCaregory.getfParent();
        String fLevel = materialCaregory.getfLevel();
        String fHasChild = materialCaregory.getfHasChild();
        if(StringUtils.isBlank(fMcShowName) || StringUtils.isBlank(fMcName)
                || StringUtils.isBlank(fParent) || StringUtils.isBlank(fLevel) || StringUtils.isBlank(fHasChild))
        {
            //表单验证
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }

        int resultNum = 0;
        if(StringUtils.isBlank(materialCaregory.getfMcId())){
            String fMcId = UUID.randomUUID().toString().replace("-","");
            materialCaregory.setfMcId(fMcId);
            resultNum = materialCaregoryService.addMc(materialCaregory);
            if(resultNum > 0 && "0".equals(fHasChild)){ //fHasChild=0 : 表示没有子节点
                MaterialCaregoryAttr materialCaregoryAttr = new MaterialCaregoryAttr();
                materialCaregoryAttr.setfMcId(materialCaregory.getfMcId());
                this.saveCommonAttr(req,materialCaregoryAttr);
            }
        }else {
            MaterialCaregory mc = materialCaregoryService.getFMcByFMcId(materialCaregory);
            if(mc == null){
                resultNum = materialCaregoryService.addMc(materialCaregory);
                if(resultNum > 0 && "0".equals(fHasChild)){ //fHasChild=0 : 表示没有子节点
                    MaterialCaregoryAttr materialCaregoryAttr = new MaterialCaregoryAttr();
                    materialCaregoryAttr.setfMcId(materialCaregory.getfMcId());
                    this.saveCommonAttr(req,materialCaregoryAttr);
                }
            }else{
                resultNum = materialCaregoryService.updateMc(materialCaregory);
            }
        }

        if(resultNum > 0)
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("保存资材分类成功");
        }
        else
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("保存资材分类失败");
        }
        return  mb;
    }

    @RequestMapping("saveMcAttr")
    @ResponseBody
    public MessageBean saveMcAttr(HttpServletRequest req,MaterialCaregoryAttr materialCaregoryAttr)
    {
        MessageBean mb = new MessageBean();
        String fMcId = materialCaregoryAttr.getfMcId();
        String fCaShowName = materialCaregoryAttr.getfCaShowName();
        String fCaName = materialCaregoryAttr.getfCaName();
        String fCaFieldType = materialCaregoryAttr.getfCaFieldType();
//        String fCaDefaultValue = materialCaregoryAttr.getfCaDefaultValue();
//        String fCaKeyField = materialCaregoryAttr.getfCaKeyField();
//        String fCaLength = materialCaregoryAttr.getfCaLength();
//        String fCaMaxValue = materialCaregoryAttr.getfCaMaxValue();
//        String fCaMinValue = materialCaregoryAttr.getfCaMinValue();
//        String fCaIsNull = materialCaregoryAttr.getfCaIsNull();
//        String fCaIsUniqueness = materialCaregoryAttr.getfCaIsUniqueness();
//        String fCaCompany = materialCaregoryAttr.getfCaCompany();
//        String fCaOrder = materialCaregoryAttr.getfCaOrder();
//        String fCaEnabled = materialCaregoryAttr.getfCaEnabled();
        String fCaCode = materialCaregoryAttr.getfCaCode();
        if(StringUtils.isBlank(fMcId) || StringUtils.isBlank(fCaShowName)
                || StringUtils.isBlank(fCaName) || StringUtils.isBlank(fCaFieldType)
//                || StringUtils.isBlank(fCaDefaultValue) || StringUtils.isBlank(fCaKeyField)
//                || StringUtils.isBlank(fCaLength) || StringUtils.isBlank(fCaMaxValue)
//                || StringUtils.isBlank(fCaMinValue) || StringUtils.isBlank(fCaIsNull)
//                || StringUtils.isBlank(fCaIsUniqueness) || StringUtils.isBlank(fCaCompany)
//                || StringUtils.isBlank(fCaOrder) || StringUtils.isBlank(fCaEnabled)
                || StringUtils.isBlank(fCaCode))
        {
            //表单验证
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }

        int resultNum = 0;
        if(StringUtils.isBlank(materialCaregoryAttr.getfCaId())){
//            String fCaId = UUID.randomUUID().toString().replace("-","");
            String fCaId = System.currentTimeMillis() + "";
            materialCaregoryAttr.setfCaId(fCaId);
            MaterialCaregoryAttr mcAttr = new MaterialCaregoryAttr();
            mcAttr.setfMcId(fMcId);
            int count = materialCaregoryAttrService.getMcAttrListCount(mcAttr);
            materialCaregoryAttr.setfCaOrder(count + 1);
            resultNum = materialCaregoryAttrService.addMcAttr(materialCaregoryAttr);
        }else {
            resultNum = materialCaregoryAttrService.updateMcAttr(materialCaregoryAttr);
        }

        if(resultNum > 0)
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("保存资材属性成功");
        }
        else
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("保存资材属性失败");
        }
        return  mb;
    }

    @RequestMapping("updateCaEnabled")
    @ResponseBody
    public MessageBean updateCaEnabled(HttpServletRequest req,MaterialCaregoryAttr materialCaregoryAttr)
    {
        MessageBean mb = new MessageBean();
        String fCaId = materialCaregoryAttr.getfCaId();
        String fMcId = materialCaregoryAttr.getfMcId();
        String fCaEnabled = materialCaregoryAttr.getfCaEnabled();
        if(StringUtils.isBlank(fCaId) || StringUtils.isBlank(fMcId) || StringUtils.isBlank(fCaEnabled))
        {
            //表单验证
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }

        int resultNum = 0;
        resultNum = materialCaregoryAttrService.updateMcAttr(materialCaregoryAttr);

        if(resultNum > 0)
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("修改资材属性状态成功");
            List<MaterialCaregoryAttr> list = materialCaregoryAttrService.getFMcAttrByParams(materialCaregoryAttr);
            mb.setData(list);
        }
        else
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("修改资材属性状态失败");
        }
        return  mb;
    }

    @RequestMapping("updateAllCaEnabled")
    @ResponseBody
    public MessageBean updateAllCaEnabled(HttpServletRequest req,MaterialCaregoryAttr materialCaregoryAttr)
    {
        MessageBean mb = new MessageBean();
        String fMcId = materialCaregoryAttr.getfMcId();
        String fCaEnabled = materialCaregoryAttr.getfCaEnabled();
        if(StringUtils.isBlank(fMcId) || StringUtils.isBlank(fCaEnabled))
        {
            //表单验证
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }

        int resultNum = 0;
        resultNum = materialCaregoryAttrService.updateFCaEnabledByMcId(materialCaregoryAttr);

        if(resultNum > 0)
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("批量修改资材属性状态成功");
        }
        else
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("批量修改资材属性状态失败");
        }
        return  mb;
    }

    @RequestMapping("updateFCaOrder")
    @ResponseBody
    public MessageBean updateFCaOrder(HttpServletRequest req,MaterialCaregoryAttr materialCaregoryAttr)
    {
        MessageBean mb = new MessageBean();
        String fMcId = materialCaregoryAttr.getfMcId();
        int fCaOrder = materialCaregoryAttr.getfCaOrder();
        String optType = materialCaregoryAttr.getOptType();//a:置顶 b:上移一位 c:下移一位 d:置底
        if(StringUtils.isBlank(fMcId) || fCaOrder <= 0 || StringUtils.isBlank(optType))
        {
            //表单验证
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }

        int resultNum = 0;


//        fCaOrder：5 optType：a 小于5的+1
//        fCaOrder：5 optType：b 等于4的改为5
//        fCaOrder：5 optType：c 等于6的改为5
//        fCaOrder：5 optType：d 大于5的-1
        if("a".equals(optType) || "d".equals(optType)){
            resultNum = materialCaregoryAttrService.updateFCaOrderByMcId(materialCaregoryAttr);
        }else {
            MaterialCaregoryAttr mca = new MaterialCaregoryAttr();
            mca.setfMcId(fMcId);
            mca.setOptType(optType);
            if("b".equals(optType)){
                mca.setfCaOrder(fCaOrder-1);
            }else if("c".equals(optType)){
                mca.setfCaOrder(fCaOrder+1);
            }
            resultNum = materialCaregoryAttrService.updateFCaOrderByMcId(mca);
        }

        if(resultNum > 0){
            if ("a".equals(optType)) {
                materialCaregoryAttr.setfCaOrder(1);
            }else if ("b".equals(optType)) {
                materialCaregoryAttr.setfCaOrder(fCaOrder-1);
            }else if ("c".equals(optType)) {
                materialCaregoryAttr.setfCaOrder(fCaOrder+1);
            }else if ("d".equals(optType)) {
                int count = materialCaregoryAttrService.getMcAttrListCount(materialCaregoryAttr);
                materialCaregoryAttr.setfCaOrder(count);
            }
            resultNum = materialCaregoryAttrService.updateMcAttr(materialCaregoryAttr);
        }

        if (resultNum > 0) {
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("批量修改资材属性排序成功");
        } else {
            int count = materialCaregoryAttrService.getMcAttrListCount(materialCaregoryAttr);
            if(fCaOrder == 1 && ("a".equals(optType) || "b".equals(optType))){
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("已经是第一个了");
            }else if(fCaOrder >= count && ("c".equals(optType) || "d".equals(optType))){
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("已经是最后一个了");
            }else{
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("批量修改资材属性排序失败");
            }
        }
        return  mb;
    }

    @RequestMapping("saveCommonAttr")
    @ResponseBody
    public MessageBean saveCommonAttr(HttpServletRequest req,MaterialCaregoryAttr materialCaregoryAttr)
    {
        MessageBean mb = new MessageBean();
        String fMcId = materialCaregoryAttr.getfMcId();

        if(StringUtils.isBlank(fMcId))
        {
            //表单验证
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }

        List<MaterialCaregoryAttr> commonAttrList = materialCaregoryAttrService.getCommonAttr(materialCaregoryAttr);
        int resultNum = 0;
        resultNum = materialCaregoryAttrService.addMcAttrBatch(commonAttrList);

        if(resultNum > 0)
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("保存资材共通属性成功");
        }
        else
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("保存资材共通属性失败");
        }
        return  mb;
    }

    @RequestMapping("getProductByFMcId")
    @ResponseBody
    public MessageBean getProductByFMcId(HttpServletRequest request)
    {
        MessageBean mb = new MessageBean();
        String fMcId = request.getParameter("fMcId");
        if(StringUtils.isBlank(fMcId))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("资材分类编号缺失");
            return mb;
        }

        Product product = new Product();
        product.setfMcId(fMcId);
        List list = productService.getProductByFMcId(product);
        if(list != null && list.size()>0)
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("该资材分类下存在设备!");
        }
        else{
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("资材分类可以删除");
        }
        return mb;
    }

    @RequestMapping("deleteMc")
    @ResponseBody
    public MessageBean deleteMc(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String fMcId = req.getParameter("fMcId");
        if(StringUtils.isBlank(fMcId))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("资材分类ID缺失");
            return mb;
        }

        MaterialCaregoryAttr materialCaregoryAttr = new MaterialCaregoryAttr();
        materialCaregoryAttr.setfMcId(fMcId);
        materialCaregoryAttrService.deleteMcAttrByFMcId(materialCaregoryAttr);
        MaterialCaregory materialCaregory = new MaterialCaregory();
        materialCaregory.setfMcId(fMcId);
        int resultNum = materialCaregoryService.deleteMc(materialCaregory);
        if(resultNum < 0)
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("删除资材分类失败");
        }
        else{
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("删除资材分类成功");
        }
        return mb;
    }

    @RequestMapping("deleteMcAttr")
    @ResponseBody
    public MessageBean deleteMcAttr(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String fCaId = req.getParameter("fCaId");
        if(StringUtils.isBlank(fCaId))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("资材属性ID缺失");
            return mb;
        }

        MaterialCaregoryAttr materialCaregoryAttr = new MaterialCaregoryAttr();
        materialCaregoryAttr.setfCaId(fCaId);
        int resultNum = materialCaregoryAttrService.deleteMcAttrByFCaId(materialCaregoryAttr);
        if(resultNum < 0)
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("删除资材属性失败");
        }
        else{
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("删除资材属性成功");
        }
        return mb;
    }
}
